package com.bianguo.android.tencentpushdemo.view.activity;

import android.Manifest;
import android.graphics.drawable.Drawable;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.events.BaseEvent;
import com.bianguo.android.tencentpushdemo.utils.DBHelper;
import com.bianguo.android.tencentpushdemo.utils.MyLog;
import com.bianguo.android.tencentpushdemo.utils.PermissionHelper;
import com.bianguo.android.tencentpushdemo.view.fragment.HomeFragment;
import com.bianguo.android.tencentpushdemo.view.fragment.ServiceFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.main_toolbar)
    Toolbar toolbar;
    @BindView(R.id.main_news)
    TextView newsTv;
    @BindView(R.id.main_service)
    TextView serviceTv;
    @BindView(R.id.main_find)
    TextView findTv;
    @BindView(R.id.main_me)
    TextView meTv;

    private ArrayList<Fragment> fragments;
    private FragmentManager mManager;
    private DBHelper dbHelper;

    @Override
    public int layoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        text.setText("你好，世界！");
        setSupportActionBar(toolbar);

        PermissionHelper.getInstance(mContext).isPermission(mContext, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);

        mManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mManager.beginTransaction();
        fragments = new ArrayList<>();
        HomeFragment fragment = HomeFragment.newInstance();
        fragments.add(fragment);
        fragmentTransaction.add(R.id.frame_container, fragment);
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();

        fragments.add(ServiceFragment.newInstance(1));
        fragments.add(ServiceFragment.newInstance(2));
        fragments.add(ServiceFragment.newInstance(3));
        newsTv.setSelected(true);
    }

    @Override
    public void initData() {
        dbHelper = new DBHelper(mContext, DBHelper.DB_VERSION);
    }

    @OnClick({R.id.text, R.id.main_news, R.id.main_service,
        R.id.main_find, R.id.main_me})
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.text:
                dbHelper.deleteAll(dbHelper.getWritableDatabase());
                checkPermissions(new String[]{});
                EventBus.getDefault().post(new BaseEvent());
                break;
            case R.id.main_news:
                if (newsTv.isSelected()) {
                    return;
                }
                newsTv.setSelected(true);
                serviceTv.setSelected(false);
                findTv.setSelected(false);
                meTv.setSelected(false);
                switchFragment(0);
                break;
            case R.id.main_service:
                if (serviceTv.isSelected()) {
                    return;
                }
                newsTv.setSelected(false);
                serviceTv.setSelected(true);
                findTv.setSelected(false);
                meTv.setSelected(false);
                switchFragment(1);
                break;
            case R.id.main_find:
                if (findTv.isSelected()) {
                    return;
                }
                newsTv.setSelected(false);
                serviceTv.setSelected(false);
                findTv.setSelected(true);
                meTv.setSelected(false);
                switchFragment(2);
                break;
            case R.id.main_me:
                if (meTv.isSelected()) {
                    return;
                }
                newsTv.setSelected(false);
                serviceTv.setSelected(false);
                findTv.setSelected(false);
                meTv.setSelected(true);
                switchFragment(3);
                break;
        }
    }

    private void switchFragment(int index) {
        int size = fragments.size();
        for (int i = 0; i < size; i++) {
            Fragment fragment = fragments.get(i);
            if (index == i) {
                if (!fragment.isAdded()) {
                    mManager.beginTransaction().add(R.id.frame_container, fragment).commit();
                }
                mManager.beginTransaction().show(fragment).commit();
            } else {
                mManager.beginTransaction().hide(fragment).commit();
            }
        }
    }

    @Override
    public void onEvent(BaseEvent event) {
        super.onEvent(event);
        text.setTextColor(ActivityCompat.getColor(mContext, R.color.colorAccent));
    }

    public void shoToolbar(int round){
        MyLog.myLog("round = " + round);
        Drawable c = toolbar.getBackground();
        c.setAlpha(round);
        toolbar.setBackgroundDrawable(c);
    }
}
